<template>
		<view class="container">
	<uni-section title="报价联盟" type="line">
		<uni-collapse accordion v-model="accordionVal">
			<uni-collapse-item  :open="false" v-for="(item,index) in list" :key="index" >	
		<template v-slot:title>
        <view  style="line-height: 50px;padding-left:20px;font-size: 18px;">{{item.name}}</view>
      </template>
				<view class="content" v-for="(v,i) in item.dataList" :key="i">
					<text class="text" style="font-size: 16px;">{{ v.channel_name }}</text>
				</view>
			</uni-collapse-item>
		</uni-collapse>
	</uni-section>
	<uni-popup ref="partner" background-color="#fff" :mask-click="false" :is-mask-click="false">
			<view class="popup-content">
				<view class="title">请选择提供报价方</view>
				<view class="list">
					<uni-list>
						<uni-list-item :clickable="true" v-for="p of partners" :key="p.id" :title="p.name" :note="p.service_area"
							@click="togglePartnerSelected(p)">
							<template v-slot:footer><uni-icons v-show="showCheckIcon(p.id)" type="checkmarkempty" size="20"></uni-icons>
</template>
						</uni-list-item>
					</uni-list>
				</view>
				<view class="footer" style="padding:15px;display: flex;flex-direction: row;justify-content: space-around;align-items: center;">
					<button @click="closePartner()" style="width:160px">取消</button>
					<button type="primary" @click="joinPartner()" style="width:160px">确定</button>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>

export default {
	data() {
		return {
			accordionVal:'1',
			list: [],
			partners: [],
			joinPartnerIds:[],
			userId:'',
		}
	},
	onLoad(event) {
		this.userId = uniCloud.getCurrentUserInfo().uid
		this.loadShop()
		this.loadShopShow()
	},
	methods: {
		showCheckIcon(id){
				return this.joinPartnerIds.indexOf(id)>=0
			},
		togglePartnerSelected(p){
				let idx = this.joinPartnerIds.indexOf(p.id)
				if(idx>=0){
					this.joinPartnerIds.splice(idx,1)
				}
				else{
					this.joinPartnerIds.push(p.id)
				}
			},
		loadShopShow() {
				this.$http.get('/web/ai/repairshop', {pid:this.$config.promotor}, (resp) => {
					this.shop = resp
					if(resp && resp.partners && resp.partners.length>0){
						this.partners = resp.partners
						this.joinPartnerIds  = this.partners.map(o=>o.id)
						this.$refs.partner.open('bottom')
					}
				})
			},
	     	closePartner(){
				this.$refs.partner.close()
			},
			joinPartner(){
				if(this.joinPartnerIds.length===0){
					uni.showModal({
						title: '提示信息',
						content: '请选择提供报价方',
						showCancel: false
					})
				}else{
					this.$http.post('/web/ai/partner/join',{ids:this.joinPartnerIds,shop_id:this.userId,promotor_id:this.$config.promotor},(resp)=>{
						this.closePartner()
						setTimeout(()=>{
							this.loadShop()
						},200)
					})
				}
			},
		loadShop() {
			this.$http.get('/web/ai/mypartners', {}, (resp) => {
				this.list = resp
				this.list.forEach(item => {
					this.setItem(item)
				})	
			})
		},
		setItem(item) {
			this.$http.get('/web/ai/partner/channels', { partner_id: item.id }, (resp) => {
				item.dataList = resp
			})
		},
	}
}
</script>
<style lang="scss">

.content {
// width: calc(100%;
padding:0 20px 10px 34px;
}

::v-deep .uni-section .uni-section-header {
	padding-left: 10px !important;
	}
::v-deep .uni-section__content-title {
font-size: 18px !important;
font-weight: bold !important;
}
.popup-content .title{
		font-size: 17px;
		padding: 10px;
		font-weight: 700;
	}
	.popup-content .list {
		max-height: 400px;
		overflow: scroll;
	}
</style>